<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Product extends MX_Controller
{
	protected $_templates;
	
	function __construct() 
	{
		parent::__construct();
		$this->pre_message = "";         
		/*$this->permit_library->check_permit();
		$this->session->set_userdata(array('Url'=>uri_string()));*/
		//load language
		//$this->load->helper('search') ;
		//$this->load->library('string_library');
		$this->lang->load('common');
		$this->load->model('product_model','product');
	}   
	
	function index($start=0)
	{
		$this->createMenu();
		
		/*Begin search*/
		$type = $this->input->post('type');
		$data['search_bar']= searchBar(array(),array('name_vn'=>'Product Name'),'product/index/');
		if($type=='ajax')
		{
			$arr_search['key_search'] = $this->input->post('key_search');
			$arr_search['field_search'] = $this->input->post('field_search');
			$this->session->set_userdata('key_search',$arr_search['key_search']);
			$this->session->set_userdata('field_search',$arr_search['field_search']);
		}
		else
		{
			$arr_search['key_search']=$this->session->userdata('key_search');
			$arr_search['field_search']=$this->session->userdata('field_search');
		}
		/*End  search*/
		
		$data['title'] = 'Sản phẩm';
		$data['add'] = 'product/add';
		$config['base_url'] = base_url().'product/index/';  
		$data['num'] = $this->product->get_num_data($arr_search); 
		$config['total_rows'] =  $data['num'];
		$config['per_page'] = 20;
		$config['uri_segment'] = 3;   
		$this->pagination->initialize($config); 
		  
		$data['list'] =   $this->product->get_all_data($config['per_page'],$start,$arr_search);
		$data['pagination']    = $this->pagination->create_links();            
	    
		$data['start'] = $start ;
		$type = $this->input->post('type');
		$data['page'] = 'index' ;
        if($type=="ajax")
            $this->load->view($data['page'],$data);
        else
            $this->load->view('layout/skin',$data);
	}
	
	function add()
	{                       
		$data['title'] = "Thêm sản phẩm";
		$data['category_option'] = $this->product->optionCategoryLevel(); //trit($data['category_option']);
		
		if(isset($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('catid',lang('category_name'),'trim|required');
			$this->form_validation->set_rules('name_vn',lang('title'),'trim|required');
			if($this->form_validation->run() == FALSE)
			{
				$this->pre_message = validation_errors();
			}
			else
			{
				$data = $this->_build_data();
				if($this->product->save($data)){
					$this->session->set_flashdata('message','Lưu thành công');
					redirect('product/index');
				}else{
					$this->pre_message = 'Lưu không thành công';
					redirect('product/add');
				}
			}
		}
		$data['message'] = $this->pre_message;
		$data['page'] = 'product/add';
		$this->load->view('layout/skin',$data);
	}
	
	function edit($id)
	{                         
		$data['title'] = lang('title_edit');
		$data['rs'] = $this->product->get_detail('product','pro_id',$id);
		$data['category_option'] = $this->product->optionCategoryLevel(); //trit($data['category_option']);
		if(isset($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('catid',lang('category_name'),'trim|required');
			$this->form_validation->set_rules('name_vn',lang('title'),'trim|required');
			if($this->form_validation->run() == FALSE)
			{
				$this->pre_message = validation_errors();
			}
			else
			{
				$req = $this->_build_data();
				//if($req['lb_image']) $this->delimg('../uploads/product/'.$data['rs']->lb_image);
				//if($req['pro_banner']) $this->delimg('../uploads/product/banner/'.$data['rs']->pro_banner);
				if($this->product->save($req,$id)){
					$this->session->set_flashdata('message','Lưu thành công');
					redirect('product/index');
				}else{
					$this->pre_message = 'Lưu không thành công';
					redirect('product/edit');
				}
			}
		}
		$data['message'] = $this->pre_message;
		$data['page'] = 'product/edit';
		$this->load->view('layout/skin',$data);
	}
	
	function _build_data($flag=1)
	{
		$req["catid"] = $this->input->post("catid",0);
		$req["name_vn"] = $this->input->post("name_vn");
		$req["name_en"] = $this->input->post("name_en");
		$req["summary_vn"] = $this->input->post("summary_vn");
		$req["summary_en"] = $this->input->post("summary_en");
		$req["content_vn"] = $this->input->post("content_vn");
		$req["content_en"] = $this->input->post("content_en");
		$req["nb_order"] = $this->input->post("nb_order");
		if($flag) $req["dt_create"] = date('Y-m-d H:i:s');
		$req["dt_modify"] = date('Y-m-d H:i:s');
		$req["bl_active"] = (int) $this->input->post("bl_active");
		
		//upload hinh anh
		$img = $this->input->post("lb_image");
		$img_name = end(explode('/', $img)) ;
		if($img)
		{
			if(file_exists('../uploads/product/'.$img_name))
			{
				//@unlink('../uploads/product/'.$img_name);
				$img_name = time().$img_name;
			}
			fn_resize_image('../uploads/'.$img,'../uploads/product/'.$img_name, 175, 175); 
			@unlink('../uploads/temp/'.$img_name);
			$req["lb_image"] = $img_name;
		}
		
		//upload banner
		$img = $this->input->post("pro_banner");
		$img_name = end(explode('/', $img)) ;
		if($img)
		{
			if(file_exists('../uploads/product/banner/'.$img_name))
			{
				//@unlink('../uploads/product/banner/'.$img_name);
				$img_name = time().$img_name;
			}
			fn_resize_image('../uploads/'.$img,'../uploads/product/banner/'.$img_name, 756, 180); 
			@unlink('../uploads/temp/'.$img_name);
			$req["pro_banner"] = $img_name;
		}
		return $req;
	}
	
	private function delimg($filename='')
	{
		if(!empty($filename) && file_exists($filename)) @unlink($filename);
	}
	
	function del($id=0,$page=0)
	{
		if($this->product->deleted($id))
			$this->session->set_flashdata('message','Đã xóa thành công');
		else $this->session->set_flashdata('message','Xóa không thành công');
			redirect('product/index/'.$page);
	}
	
	function dels()
	{
		if(!empty($_POST['ar_id']))
		{
			$page = (int)$this->input->post('page');
			$ar_id = $this->input->post('ar_id');
		
			if(!empty($_POST['btn_submit']))
			{
				for($i = 0; $i < sizeof($ar_id); $i ++) {
					if ($ar_id[$i]){
						if($this->product->deleted($ar_id[$i]))
							$this->session->set_flashdata('message','Đã xóa thành công');
						else $this->session->set_flashdata('message','Xóa không thành công');
					}
				}
			}
		}
		redirect('product/index/'.$page);
	}
	
	function createMenu()
	{
		$arrCat=$this->product->get_menu(50);
		
		$str_vn = "<div class='item'>\n\t<ul>\n";
		$str_en = "<div class='item'>\n\t<ul>\n";
		if($arrCat)
		{
			 //$icon = base_url_site().'/templates/images/index-pro.gif" width="4" height="4' ;
			 foreach($arrCat as $cat)
			 {
				$links_vn = DOMAIN.'/vn/sanpham/detail/'.$cat->pro_id.'/'.kodau($cat->name_vn).'.html';
			 	$links_en = DOMAIN.'/vn/sanpham/detail/'.$cat->pro_id.'/'.kodau($cat->name_en).'.html';
				$str_vn.= "\t\t".'<li><a href="'.$links_vn.'">'.$cat->name_vn.'</a></li>'."\n";
				$str_en.= "\t\t".'<li><a href="'.$links_vn.'">'.$cat->name_en.'</a></li>'."\n";
			 }
		}
		$str_vn = $str_vn."\t</ul>\n<div>"; 
		$str_en = $str_en."\t</ul>\n<div>";
		
		$dir_vn = DIR_DATA.'/mn_popup_vn.php';
		$dir_en = DIR_DATA.'/mn_popup_en.php';
		write_file($dir_vn, $str_vn);
		write_file($dir_en, $str_en);
		return true;
	}
}
?>
